//
// Created by shiminli on 2024/1/4.
//

#ifndef DDOFFICE_SQLBUILDER_H
#define DDOFFICE_SQLBUILDER_H

#include <iostream>
#include "SQLHelper.h"
#include <mysql/mysql.h>
#include <vector>

using namespace std;

struct ValuePair{

    bool isString;
    string value;
    bool isTime;

    ValuePair(bool isString, string value){
        this->isString = isString;
        this->value = value;
        this->isTime = false;
    };

    ValuePair(bool isString, string value, bool isTime){
        this->isString = isString;
        this->value = value;
        this->isTime = isTime;
    };
};

namespace ddoffice{
    class SQLBuilder {
    public:
        static bool sisd(std::string aa) {
            try {
                size_t pos;
                double doubleValue = std::stod(aa, &pos);
                if (pos == aa.size()) {
                    long intValue = static_cast<long>(doubleValue);
                    return doubleValue != intValue;
                } else {
                    std::cout << "这个值是浮点型。" << std::endl;
                    return 1;
                }
            } catch (const std::invalid_argument& e) {
                std::cout << "无法将字符串转换为数字。" << std::endl;
            } catch (const std::out_of_range& e) {
                std::cout << "数字超出范围。" << std::endl;
            }

            return 0;
        }

        static string insertSql(const string &tabName, const vector<string> &columns, const vector<ValuePair> &values) {
            if (columns.size() != values.size()) {
                // 处理列名和值的数量不匹配的情况
                return NULL;
            }

            string cmd = "INSERT INTO " + tabName + " (";
            for (size_t i = 0; i < columns.size(); i++) {
                cmd += columns[i];
                if (i < columns.size() - 1) {
                    cmd += ", ";
                }
            }
            cmd += ") VALUES (";
            for (size_t i = 0; i < values.size(); i++) {
                if (values[i].isString){
                    cmd += "'" + values[i].value + "'";
                }else if (values[i].isTime){
                    cmd.append("STR_TO_DATE('").append(values[i].value).append("','%Y-%m-%d %H:%i:%s.%f')");
                }else{
                    cmd += values[i].value;
                }
                if (i < values.size() - 1) {
                    cmd += ", ";
                }
            }
            cmd += ");";

            // 返回完整的INSERT INTO语句
            return cmd;
        }

    };
}



#endif //DDOFFICE_SQLBUILDER_H
